package br.com.conciliador.util;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.export.JExcelApiExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;
import br.com.conciliador.download.vo.ExportarDownloadVo;
import br.com.r2.exception.R2Exception;

public class Download
{

    public static void exportarArquivo(ExportarDownloadVo exportarDownloadVo) throws R2Exception
    {
       
       FileWriter writer = null;
       PrintWriter saida = null;
       
       try
      {
         writer = new FileWriter(new File(exportarDownloadVo.getCaminhoDestino(), exportarDownloadVo.getNomeArquivoDestino()));
         saida = new PrintWriter(new BufferedWriter(writer, 1*1024*1024));

         String caminhoCompleto = exportarDownloadVo.getCaminhoOrigem() + File.separator + exportarDownloadVo.getNomeArquivoOrigem();
         
         Arquivo arquivo = new Arquivo();
         //arquivo.abrir(caminhoCompleto, true);
         arquivo.ler(caminhoCompleto);
         String linha = "";
         while((linha = arquivo.getLinha()) != null)
         {
            saida.print(linha);
            saida.print("\n");
         }
      }
       catch (IOException e)
       {
          throw new R2Exception(e);
       }
       finally
       {
          if (saida != null)
          {
             saida.close();
          }
          if (writer != null)
          {
             try
             {
                writer.close();
             }
             catch (IOException e) 
             {
                throw new R2Exception(e);
             }
          }
       }
    }
    
   public static byte[] exportarExcel(JasperPrint jasperPrint) throws R2Exception
   {
      try
      {
         JExcelApiExporter jExcelExporter = new JExcelApiExporter();
         ByteArrayOutputStream xlsReport = new ByteArrayOutputStream();
         jExcelExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
         jExcelExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, xlsReport);
         jExcelExporter.setParameter(JRXlsExporterParameter.IS_DETECT_CELL_TYPE, Boolean.TRUE);
         jExcelExporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
         jExcelExporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
         jExcelExporter.exportReport();

         byte[] bs = xlsReport.toByteArray();

         xlsReport.close();

         return bs;
      }
      catch (IOException e)
      {
         throw new R2Exception(e);
      }
      catch (JRException e)
      {
         throw new R2Exception(e);
      }
   }
}
